package com.itly.controller;

import com.itly.util.ApiResult;
import com.itly.entity.Score;
import com.itly.service.ScoreService;
import com.itly.util.ApiResultHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ScoreController {
    private Logger logger = LoggerFactory.getLogger(ScoreController.class);

    @Autowired
    private ScoreService scoreService;

    @GetMapping("score/{studentId}")
    public ApiResult score(@PathVariable("studentId") int studentId) {
        List<Score> score = scoreService.getScoreByStudentId(studentId);
        if (score.size() == 0) {
            logger.info("未查到成绩",studentId);
            return ApiResultHandler.buildApiResult(400, "未查到成绩", score);
        }
        logger.info("查询成绩成功");
        return ApiResultHandler.buildApiResult(200, "查询成功", score);
    }

    @GetMapping("scores/{examCode}")
    public ApiResult scores(@PathVariable("examCode") int examCode) {
        List<Score> score = scoreService.getScoreByExamCode(examCode);
        logger.info("通过考试查询成绩成功");
        return ApiResultHandler.buildApiResult(200, "查询成功", score);
    }

    @PostMapping("score")
    public ApiResult getScore(@RequestBody Score score) {
        int i = scoreService.addScore(score);
        if (i == 1) {
            logger.info("成绩添加成功");
            return ApiResultHandler.buildApiResult(200, "增加成绩成功", i);
        } else {
            logger.info("成绩添加失败",score);
            return ApiResultHandler.buildApiResult(400, "增加成绩失败", i);
        }
    }
}
